import Common from '../common'
var Js = Common('sql')
export default {
  name: 'sql',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: "time",
          label: '备份时间',
          application: [],
          componentName: '',
          listType: 'normal',
        },{
          key: "size",
          label: '备份大小',
          application: [],
          componentName: '',
          listType: 'normal',
          formatter:function(val){
            return val.size+'kb'
          },
          width:280
        },
        {
          label: '操作',
          listType: 'deal',
          width: 80
        },
      ],

      // 按钮配置
      btn_info: [{
          type: 'info',
          icon: 'edit',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '生成备份'
          },
          func: {
            apiName: function(self) {
              return "api_backSql"
            },
            formData: function(self) {
              var data = {};
              return data
            },
            postData: function(self) {
              var postData = {
                type: 'backall'
              };
              return postData;
            }
          },
        },
        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          styleFunc: function(data) {

          },
          func: {
            apiName: function(self) {
              return "api_backSql"
            },
            formData: function(self) {
              return self.formData
            },
            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
              	deleteArray.push(self.selectionArray[i].timestamp);
              };
              var postData = {
                type: 'delFile',
                time: ['in',deleteArray]
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          funcType: 'func',
          text: function(data) {
            return '下载'
          },
          func: {
            formData: function(self) {
              return self.formData
            },
            func: function(self) {
              self.donwLoad(self.formData);
            }
          },
        },
      ],

      searchItem: {

      },
      startSearchItem: {

      },
      optionData: {
        labelOptions: [],
        statusOptions: [{
          text: '启用',
          value: 1
        }, {
          text: '禁用',
          value: -1
        }],
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      }

    }

  },

  mounted() {
    const self = this;
    self.searchData = self.$$cloneForm(self.searchItem);
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMainData();
    },

    clearSearch() {
      const self = this;
      self.searchItem = {
        thirdapp_id: this.$store.getters.getUserinfo.thirdapp_id
      };
      self.initMainData(true);
    },

    /**
     * 列表主函数
     */
    async initMainData(isNew) {

      const self = this;
      self.table_arguments.loading = true;
      const postData = {
        type: 'fileList'
      };
      var res = await self.$$api_backSql({
        data: postData
      });
      self.mainData = [];
      for (var key in res.info) {
        console.log(key)
        console.log(res.info[key])
        self.mainData.push({
          time: key,
          timestamp: res.info[key].time,
          size: res.info[key].size
        })
      };
      self.paginate.count = res.info.total;
      self.table_arguments.loading = false;

    },


    donwLoad(item) {
      console.log('time', item)
      var url =
        'https://test.solelyplus.com/285/public/index.php/api/v1/Project/Solely/backSql?type=downloadFile&time=' +
        item.timestamp; // 获取图片地址
      window.location.href = url;
    }

  },


}
